﻿#include "tcp_client.h"
#include <thread>
#include <chrono>

// 登录业务数据生成函数
//std::vector<uint8_t> createLoginBusinessData() {
//    std::vector<uint8_t> loginData = {
//        0x01, 0x00,  // 登录类型标识
//    };
//    return loginData;
//}

int main() {
    try {
        // 创建TCP客户端，连接到本地8080端口
        TcpClient client("127.0.0.1", "8080");
        bool isLoggedIn = false;

        while (true) {
            if (!client.IsConnected()) {
                std::cout << "尝试连接服务器..." << std::endl;
                if (client.Connect()) {
                    std::cout << "连接成功，准备发送登录请求..." << std::endl;
                    isLoggedIn = false;  // 重连后需要重新登录
                }
                else {
                    std::this_thread::sleep_for(std::chrono::seconds(1));
                    continue;
                }
            }

            if (!isLoggedIn) {
				std::vector<uint8_t> loginData = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 }; // 示例登录数据
                std::cout << "正在发送登录请求：1111111111111111111111111111" << std::endl;
                if (client.SendBusinessData(loginData,0x01,0x03)) {
                    std::cout << "登录请求已发送，等待响应...111111111111111111111111" << std::endl;
                    // 等待登录响应
                    Message loginResponse;
                    if (client.ReceiveMessage(loginResponse)) {  // 3秒超时
                        if (loginResponse.logicHeader.mainCommand == 0x01 &&
                            loginResponse.logicHeader.direction == 0x13) {  // 假设0x13是登录成功ACK
                            std::cout << "登录成功！终端已在线" << std::endl;
                            isLoggedIn = true;
                        }
                        else {
                            std::cout << "登录响应不符合预期，将重试..." << std::endl;
                            std::this_thread::sleep_for(std::chrono::seconds(1));
                        }
                    }
                    else {
                        std::cout << "登录响应超时，将重试..." << std::endl;
                        client.Disconnect();  // 断开连接，触发重连
                    }
                }
                else {
                    std::cout << "登录请求发送失败，将重试..." << std::endl;
                    std::this_thread::sleep_for(std::chrono::seconds(1));
                }
            }
            else {
                // 已登录状态，处理服务器请求
                Message serverMessage;
                if (client.ReceiveMessage(serverMessage)) { 
                    std::cout << "收到服务器消息，主命令: 0x" << std::hex
                        << static_cast<int>(serverMessage.logicHeader.mainCommand) << std::dec << std::endl;
                    client.ProcessReceivedMessage(serverMessage);  // 自动处理ACK回复
                }

                //其他业务逻辑，每10秒发送一次心跳
                static auto lastHeartbeat = std::chrono::steady_clock::now();
                auto now = std::chrono::steady_clock::now();
                if (std::chrono::duration_cast<std::chrono::seconds>(now - lastHeartbeat).count() >= 10) {
                    std::vector<uint8_t> heartbeatData = {};  // 简单心跳数据
                    std::cout << "正在发送心跳包：6666666666666666666666666666666666" << std::endl;
                    if (client.SendBusinessData(heartbeatData, 0x06, 0x03)) {
                        lastHeartbeat = now;
                        std::cout << "已成功发送心跳包6666666666666666666666666666666666666" << std::endl;
                    }
                    
                }
            }
        }
    }
    catch (std::exception& e) {
        std::cerr << "发生错误：" << e.what() << std::endl;
        return 1;
    }

    return 0;
}
